package com.woniuxy.controller;


import cn.hutool.core.io.FileUtil;

import com.woniuxy.common.Result;
import com.woniuxy.controller.form.StoreInfoSearchForm;
import com.woniuxy.controller.form.StoreInfoUpdateForm;
import com.woniuxy.controller.form.StoreInfoUpdateStatusForm;
import com.woniuxy.entity.StoreInfo;
import com.woniuxy.service.StoreInfoService;
import com.woniuxy.service.dto.CategoryDto;
import com.woniuxy.service.dto.StoreInfoForAdminDto;
import com.woniuxy.service.dto.StoreInfoForOwnerDto;
import com.woniuxy.service.dto.WoniuPageDto;
import com.woniuxy.util.GetTypeByHead;
import com.woniuxy.util.OSSUtil;
import com.woniuxy.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/storeInfo")
@Api(tags = "商家详情相关接口")
public class StoreInfoController {

    @Autowired
    private StoreInfoService service;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OSSUtil ossUtil;

    @PostMapping("/update")
    @ApiOperation("更新基本信息")
    public Result updateMessage(@RequestBody StoreInfoUpdateForm form, @RequestHeader String accessToken){
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        StoreInfo storeInfo =service.updateMessage(form,accountId);
        return Result.success(200,"商家信息更新成功",storeInfo);
    }



    @PostMapping("/search")
    @ApiOperation("条件查询商家信息")
    public Result search(@RequestBody StoreInfoSearchForm form) {
        WoniuPageDto<StoreInfoForAdminDto> dtoPageInfo = service.searchByCondition(form);
        return Result.success(200,"条件查询商家信息成功",dtoPageInfo);
    }


    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result upload(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        if ( !(fileType.equals("jpg")|| fileType.equals("png")|| fileType.equals("tif") ||fileType.equals("jpeg"))){
            throw new Exception("请上传图片类型的文件");
        }
        InputStream is = file.getInputStream();
        String fileOriginalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileOriginalFilename);
        String newFileName = UUID.randomUUID().toString().replace("_","");
        String url = ossUtil.uploadFile(is, newFileName + "." + suffix, "bucket-book-fjx");
        return Result.success(200,"上传图片成功",url);
        //https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/606b7e7c-addf-4e65-8b2f-7e4bea2b4157.jpeg
    }

    @PostMapping("/uploadfile")
    @ApiOperation("上传附件文件")
    public Result uploadFile(MultipartFile file) throws Exception {
        InputStream is = file.getInputStream();
        String fileOriginalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileOriginalFilename);
        String newFileName = UUID.randomUUID().toString().replace("_","");
        String url = ossUtil.uploadFile(is, newFileName + "." + suffix, "bucket-book-fjx");
        return Result.success(200,"上传文件成功",url);
    //https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/447559c7-e787-4627-8360-974a9122af3b.pdf
    }

    @PostMapping("/updatestatus")
    @ApiOperation("冻结或激活或删除商家")
    public Result updateStatus(@RequestBody StoreInfoUpdateStatusForm form){
        service.updateStatus(form);
        if(form.getDeleteFlag()==null){
            return Result.success(200,"冻结或激活商家成功",null);
        }else{
            return Result.success(200,"删除商家成功",null);
        }

    }

    @GetMapping("/updatestatusbyowner")
    @ApiOperation("商家歇业或开业")
    @ApiImplicitParam(name = "0：歇业；1：开业",value = "storeStatus")
    public Result updateStatusByOwner(Integer storeStatus,@RequestHeader String accessToken){
        Integer accessId = redisUtil.getUserIdByToken(accessToken);
        service.updateStatusByOwner(storeStatus,accessId);
        return Result.success(200,"商家开业或歇业成功",null);
    }

    @GetMapping("/one")
    @ApiOperation("获得当前登录的商家信息")
    public Result getOne(@RequestHeader String accessToken){
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        StoreInfoForOwnerDto dto = service.get(accountId);
        return Result.success(200,"获得当前商家信息成功",dto);
    }

    @GetMapping("/categories")
    @ApiOperation("获取商家类别")
    public Result categories(@RequestHeader String accessToken) {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        List<CategoryDto> categories = service.getCategories(accountId);
        return Result.success(200,"ok", categories);
    }

    @GetMapping("/all-categories")
    @ApiOperation("获取所有类别")
    public Result allCategories() {
        List<CategoryDto> categories = service.getCategories();
        return Result.success(200,"ok", categories);
    }

}

